package cn.edu.bit.ss;

import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.edu.bit.dto.Source;
import cn.edu.bit.dto.WebPage;
import cn.edu.bit.ss.dao.UpdateDAO;
import cn.edu.bit.ss.parser.*;
import cn.edu.bit.ss.spider.BroadPrioSpider;

public class UpdateSpider {
	
	private DefaultParser parser;
	private BroadPrioSpider spider;

	private UpdateDAO dao;
	
	public UpdateSpider(DefaultParser parser, BroadPrioSpider spider,
			UpdateDAO dao) {
		super();
		this.parser = parser;
		this.spider = spider;
		this.dao = dao;
	}

	public static void main(String[] args){
		String[] locations = {"applicationContext.xml"};
	    ApplicationContext ctx = new ClassPathXmlApplicationContext(locations);
	    
	    UpdateSpider us = new UpdateSpider(new DefaultParser(),new BroadPrioSpider(),(UpdateDAO)ctx.getBean("updateDAO"));
	    
    	for(int offset = 0,limit = 10;;offset+=10){
    		ArrayList<String> urls = us.dao.getSomeUrls(offset, limit);
    		if(urls==null || urls.isEmpty()){
    			//break;
    			try {
					Thread.sleep(3*24*60*60*1000);
					offset = 0;
					continue;
				} catch (InterruptedException e) {
					
				}
    		}
    		
    		try {
    			for(String url : urls){
					Source sc = new Source(url,us.spider.getSource(url));
					us.dao.update(new WebPage(url,us.parser.getTitle(sc),us.parser.getContent(sc)));
    			}
			} catch (Exception e) {
				//ignore errors
    		}
	    }
	    
	    
	}
}
